package cst.a526whx.xm_526whx.model;

import java.util.List;

import cst.a526whx.xm_526whx.bean.ArticleBean;
import cst.a526whx.xm_526whx.bean.LoginBean;
import cst.a526whx.xm_526whx.iface.AmicoolIface;
import cst.a526whx.xm_526whx.iface.AmicoolListener;

import cst.a526whx.xm_526whx.service.AmicoolService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


public class AmicoolModel implements AmicoolIface {//先得到BASEURL，在通过builder来构建链接，baseUrl（BASEURL）访问网址，
    // addConverterFactory来解析数据，最后build解析后的数据
    private Retrofit retrofit;
    private AmicoolService service;
    private static final String BASE_URL="http://amicool.neusoft.edu.cn/";
    public AmicoolModel(String type) {
        if(type .equals("gson") ){
            retrofit=new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        else {
            retrofit=new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service=retrofit.create(AmicoolService.class);

    }
    @Override
    public void login(String u, String p, final AmicoolListener l) {
        Call<LoginBean> call=service.login(u,p);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    l.onResponse(response.body());
                }
                else
                    l.onFail("用户不存在");
            }
            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void reg(String u, String p, String t, final AmicoolListener l) {
        Call<String> call=service.reg(u,p,t);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    l.onResponse(response.body());
                }
                else
                    l.onFail("注册失败");
            }
            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getList(String mod, int page, String session,final AmicoolListener l) {
        Call<List<ArticleBean>> call = service.getArticleList(mod,page,session);
        switch (mod){
            case "article":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            l.onResponse(response.body());
                        } else {
                            l.onFail("Error！");
                        }
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "video":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "tware":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "tcase":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "svideo":
                Call<List<ArticleBean>> svideo= service.getSVideoList("video",page,session);
                svideo.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "project":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
        }

    }

    @Override
    public void getDetail(String mod, int id, String s, AmicoolListener l) {

    }
}
